package cn.xshi.log.web;

import cn.xshi.log.form.LogErrorForm;
import cn.xshi.log.model.LogError;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.xshi.common.annotation.Auth;
import cn.xshi.common.base.BaseAction;
import cn.xshi.common.base.BasePage;
import cn.xshi.common.base.BaseResult;
import cn.xshi.common.entity.OauthAccountEntity;
import cn.xshi.common.util.StringUtil;
import cn.xshi.log.service.LogErrorService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Desc 异常日志
* @Author 邓纯杰
* @CreateTime 2022-08-24 11:24:05
*/
@RestController
@RequestMapping("/logError")
@Api(value = "异常日志API",tags = "异常日志API",description = "异常日志")
public class LogErrorController extends BaseAction{

	@Resource
	private LogErrorService logErrorService;

	/**
	 * 查询并分页
	 * @param logErrorForm
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@ApiOperation(value="查询并分页", notes="查询并分页")
	@GetMapping(value="/list")
	@Auth(value = "/logError/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BasePage<List<LogError>> getLogErrorListByCondition(LogErrorForm logErrorForm,Integer pageNo, Integer pageSize){
		commonHPagerByPageNo(pageNo,pageSize);
		List<LogError> logErrorList = logErrorService.getLogErrorListByCondition(logErrorForm);
		for(LogError logError : logErrorList){
			if(!StringUtil.isEmpty(logError.getCreateId())){
				OauthAccountEntity createBy = getAccount(logError.getCreateId());
				if(null != createBy){
					logError.setCreateBy(createBy.getName());
				}
			}
		}
		PageInfo<LogError> page = new PageInfo<LogError>(logErrorList);
		return outPageBootByPageNo(page,pageNo,pageSize);
	}

	/**
	* 查询单条记录
	* @param id 
	*/
	@ApiOperation(value="查询单条记录", notes="查询单条记录")
	@GetMapping(value="/get/{id}")
	@Auth(value = "/logError/get",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult<LogError> getLogErrorById(@PathVariable("id")String id){
		LogError logError = logErrorService.getLogErrorById(id);
		if(!StringUtil.isEmpty(logError.getCreateId())){
			OauthAccountEntity createBy = getAccount(logError.getCreateId());
			if(null != createBy){
				logError.setCreateBy(createBy.getName());
			}
		}
		return outDataStr(logError);
	}

	/**
	* 添加
	* @param logError 
	*/
	@ApiOperation(value="添加", notes="添加")
	@PostMapping(value="/add")
	@Auth(value = "/logError/add")
	public BaseResult addLogError(@RequestBody LogError logError){
		int i = 0;
		if(null != logError){
			logError.setId(toUUID());
			i=logErrorService.addLogError(logError);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 删除
	 * @param id
	 */
	@ApiOperation(value="删除", notes="删除")
	@DeleteMapping(value="/delete")
	public BaseResult delLogError(String id){
		int i = 0;
		if(!StringUtil.isEmpty(id)){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("id",id.split(","));
			i=logErrorService.delLogError(condition);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}
}
